<?PHP
	// *******************************************************************************************
	// @Namespace : Colibri.Event
	// @Description : Define a event dispatcher.
	// @Author : Ronald Guillen
	// *******************************************************************************************
	
	class EventDispatcher
	{
		private $EventQueue = Array();
		
		function __construct()
		{
			// Not implemented
		}
		
		function addEventListener($event, $handler)
		{
			array_push($this->EventQueue, Array($event => $handler));
		}
		
		// @ Method
		function removeEventListener($event, $handler)
		{
			foreach($EventQueue as $key => $value)
			{
				if($value == Array($event => $handler))
				{
					unset($EventQueue[$key]);
				}
			}
		}
		
		// @ Method
		function dispatchEvent($eventName, $eventObject)
		{
			foreach($this->EventQueue as $key => $value)
			{		
				$eventKey = array_keys($value);
				
				if($eventKey[0] == $eventName)
				{
					if ( method_exists ($this, $value[$eventName]) === true )
           			{
            			call_user_func_array(Array($this, $value[$eventName]), Array("eventObject" => $eventObject) );
           			}
           			else
           			{   
           				exit( "El controlador de eventos <b>".$value[$eventName]."</b> no existe.");
           			}
				}
           	}
		}
	}
	
?>